package com.tcs.test.designmodel.observer;

import com.google.common.collect.Lists;

import java.util.List;

/**
 * @ClassName : WathcerData
 * @Author : 神经刀
 * @Date : 2019/5/28 : 18:37
 * @Version : 1.0
 */
public class WathcerData implements Subject {

    private List<Observer> observerList = Lists.newArrayList();
    private float temperature;
    private float humidity;
    private float pressure;

    @Override
    public void registerObserver(Observer o) {
        observerList.add(o);
    }

    @Override
    public void removeObserver(Observer o) {
        int observerIndex = observerList.indexOf(o);
        if (observerIndex > -1) {
            observerList.remove(observerIndex);
        }
    }

    @Override
    public void notifyObservers() {
        for (Observer observer : observerList) {
            observer.update(this.temperature , this.humidity , this.pressure);
        }
    }

    public void measurementsChanged() {
        this.notifyObservers();
    }

    public void setMeasurements(float temperature, float humidity, float pressure) {
        this.temperature = temperature;
        this.humidity = humidity;
        this.pressure = pressure;
        measurementsChanged();
    }
}
